package com.wifipositioner.positioning;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import android.os.Environment;

import com.wifipositioner.Globals;
import com.wifipositioner.measure.MeasuredPoint;
import com.wifipositioner.measure.MeasuredPointList;

public class RefPointCache implements Serializable{
	private HashMap<String, ReferencePointMap> dominantCache = new HashMap<String, ReferencePointMap>();
	private HashMap<String, ReferencePointMap> secDominantCache = new HashMap<String, ReferencePointMap>();
	private ArrayList<MeasuredPoint> refPointList = new ArrayList<MeasuredPoint>();
	private static RefPointCache instance = null;
	
	private  RefPointCache() {
		
	}
	public static RefPointCache get(){
		if(instance == null){
			instance = new RefPointCache();
		}
		return instance;
	}
	

	public void saveCacheToFile(MeasuredPointList measuredPointList, String fileNameToWrite)
	throws Exception{
		for(MeasuredPoint measuredPoint : measuredPointList){
			String[] mDominantSSIDs = measuredPoint.getDominantSSIDs();
			refPointList.add(measuredPoint);
			if(dominantCache.containsKey(mDominantSSIDs[0])){
				ReferencePointMap refpointMap = dominantCache.get(mDominantSSIDs[0]);
				refpointMap.put(measuredPoint.getName(), measuredPoint);
			}
			else{
				ReferencePointMap map = new ReferencePointMap();
				map.put(measuredPoint.getName(), measuredPoint);
				dominantCache.put(mDominantSSIDs[0], map);
			}
			
			if(secDominantCache.containsKey(mDominantSSIDs[1])){
				ReferencePointMap refpointMap = secDominantCache.get(mDominantSSIDs[1]);
				refpointMap.put(measuredPoint.getName(), measuredPoint);
			}
			else{
				ReferencePointMap map = new ReferencePointMap();
				map.put(measuredPoint.getName(), measuredPoint);
				secDominantCache.put(mDominantSSIDs[1], map);
			}
		}
		File root = Environment.getExternalStorageDirectory();
		FileOutputStream fos = new FileOutputStream(new File(root, Globals.mapPath + fileNameToWrite));
		ObjectOutputStream os = new ObjectOutputStream(fos);
		os.writeObject(this);
		fos.close();
		os.close();
	}
	public void loadCacheFromFile(String fileNameToRead) throws Exception{
		File root = Environment.getExternalStorageDirectory();
		FileInputStream fis = new FileInputStream(new File(root, Globals.mapPath + fileNameToRead));
		ObjectInputStream is = new ObjectInputStream(fis);
		instance = (RefPointCache) is.readObject();
		fis.close();
		is.close();
	}
	public ReferencePointMap getRefPointMapByDominantSSID(String dominantSSID){
		return dominantCache.get(dominantSSID);
	}
	public ReferencePointMap getRefPointMapBySecDominantSSID(String secDominantSSID){
		return secDominantCache.get(secDominantSSID);
	}
	public ArrayList<MeasuredPoint> getRefPointList() {
		return refPointList;
	}
	public void clearCache() {
		instance = null;
	}
}
